package edu.unh.sustain;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.ExpandableListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.Spinner;
import android.widget.TextView;

/**
 * Displays route 4A of Wildcat Transit as an expandable list.  This means
 * that each stop will appear as a top level "group", and after it is touched
 * it will expand to show the different times that the bus will service
 * that stop.
 * 
 * Displays schedule for route 4A on weekdays, weekends, and reduced service.
 * @author Blake Janelle
 *
 */
public class Route4aActivity extends ExpandableListActivity {
	
	String timeframe;
	Context ct = this;
	int tabNum = 0;
	int spinnerState = 0;
	HashMap<String,Integer> runs = new HashMap<String,Integer>();
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    
	    timeframe = getTimeframe(savedInstanceState);
	    ArrayAdapter<CharSequence> adapter = null;
	    this.addStringRuns();
	    setContentView(R.layout.route4ab);
	    
	    // set up the spinner that controls the current timeframe
	    adapter = ArrayAdapter.createFromResource(
	            this, R.array.r3_timeframes, android.R.layout.simple_spinner_item);
	    Spinner spinner = (Spinner) findViewById(R.id.timeframes);
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    spinner.setAdapter(adapter);
	    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
	    
	    // set up an expandable list that groups every stop, and when pressed, expands to display each stop time
	    SimpleExpandableListAdapter lstAdapter =
				new SimpleExpandableListAdapter
				(
					this,
					createGroupList(), 						// Creating group List.
					R.layout.expand_group_row,				// Group item layout XML.
					new String[] { "Group Item" },			// the key of group item.
					new int[] { R.id.row },				// ID of each group item.-Data under the key goes into this TextView.
					createChildList(),						// childData describes second-level entries.
					R.layout.expand_child_row,				// Layout for sub-level entries(second level).
					new String[] {"Sub Item"},				// Keys in childData maps to display.
					new int[] { R.id.grp_child }			// Data under the keys above go into these TextViews.
				);
					
	    ExpandableListView a = (ExpandableListView)findViewById(android.R.id.list);
	    a.setAdapter(lstAdapter);
		
	    // set text box labels for the corresponding timeframe
	    if( timeframe.equals("weekends"))
	    {
	    	spinnerState = 1;
	    	TextView title = (TextView)findViewById(R.id.textView1);
	    	title.setText(R.string.a4w_title);
	    	TextView route = (TextView)findViewById(R.id.textView2);
	    	route.setText(R.string.a4w_route);
	    	TextView range = (TextView)findViewById(R.id.textView3);
	    	range.setText(R.string.a4w_range);
		    spinner.setSelection(spinnerState);
	    }
	    else if( timeframe.equals("reduced"))
	    {
	    	spinnerState = 2;
	    	TextView title = (TextView)findViewById(R.id.textView1);
	    	title.setText(R.string.a4r_title);
	    	TextView route = (TextView)findViewById(R.id.textView2);
	    	route.setText(R.string.a4r_route);
	    	TextView range = (TextView)findViewById(R.id.textView3);
	    	range.setText(R.string.a4r_range);
		    spinner.setSelection(spinnerState);
	    }
	    else
	    {
	    	TextView title = (TextView)findViewById(R.id.textView1);
	    	title.setText(R.string.a4_title);
	    	TextView route = (TextView)findViewById(R.id.textView2);
	    	route.setText(R.string.a4_route);
	    	TextView range = (TextView)findViewById(R.id.textView3);
	    	range.setText(R.string.a4_range);
		    spinner.setSelection(spinnerState);
	    }
	}
	
	/**
	 * Create list of stops which are the "groups" of the expandable list
	 * 
	 * @return List<HashMap<String,String>>
	 */
	private List<HashMap<String,String>> createGroupList()
	{
		String[] stops;
				
		if(timeframe.equals("weekdays"))
			stops = getResources().getStringArray(R.array.a4stops);
		else if(timeframe.equals("weekends"))
			stops = getResources().getStringArray(R.array.a4wstops);
		else
			stops = getResources().getStringArray(R.array.a4rstops);
		
		ArrayList<HashMap<String,String>> result = new ArrayList<HashMap<String,String>>();
		  
		for( int i = 0 ; i < stops.length; i++ )
		{
			HashMap<String,String> m = new HashMap<String,String>();
			m.put( "Group Item", stops[i] );
			result.add( m );
		}
		
		return (List<HashMap<String,String>>)result;
    }

	/**
	 * Create list of times for each stop.
	 * 
	 * @return List<ArrayList<HashMap<String,String>>>
	 */
	private List<ArrayList<HashMap<String,String>>> createChildList()
	{
		String[] stops;
		String prefix;
		
		if(timeframe.equals("weekdays"))
		{
			stops = getResources().getStringArray(R.array.a4stops);
			prefix = "a4stop";
		}
		else if(timeframe.equals("weekends"))
		{
			stops = getResources().getStringArray(R.array.a4wstops);
			prefix = "a4wstop";
		}
		else
		{
			stops = getResources().getStringArray(R.array.a4rstops);
			prefix = "a4rstop";
		}
		
		ArrayList<ArrayList<HashMap<String,String>>> result = new ArrayList<ArrayList<HashMap<String,String>>>();
		
		for( int i = 0 ; i < stops.length; i++ )
		{
			String index = prefix + (i+1);
			String[] run = getResources().getStringArray(runs.get(index));
			ArrayList<HashMap<String,String>> secList = new ArrayList<HashMap<String,String>>();
			
			for( int n = 0 ; n < run.length ; n++ )
			{
				HashMap<String,String> child = new HashMap<String,String>();
				child.put( "Sub Item", run[n] );
				secList.add( child );
			}
			
			result.add( secList );
		}
		return result;
	}
	
	/**
	 * Saves the state of the screen.  This is called on orientation changes. 
	 */
	public void onSaveInstanceState(Bundle savedInstanceState)
	{
		  savedInstanceState.putString("timeframe", timeframe);
		  super.onSaveInstanceState(savedInstanceState);
	}
	
	/**
	 * Extracts the timeframe data from the Instance and returns it
	 * 
	 * @param savedInstanceState previous state
	 * @return the string or null
	 */
	private String getTimeframe(Bundle savedInstanceState)
	{
		String ret = null;
		
		if( savedInstanceState != null )
	    {
	    	ret = (String) savedInstanceState.getString("timeframe");
	    }
	    else
	    {
	    	Bundle extras = getIntent().getExtras();
	    	
	    	if( extras != null )
	    		ret = extras.getString("timeframe");
	    }
		
		return ret;
	}
	
	/**
	 * Returns the Sting equivalent of the spinner position
	 * @param current spinner position
	 * @return String equivalent
	 */
	private String getPos(int current)
	{
		if(current == 0)
			return new String("weekdays");
		else if(current == 1)
			return new String("weekends");
		else
			return new String("reduced");
	}
	
	/**
	 * A "run array" is a string array of times when each
	 * stop is serviced.  Each run array id (references route3.xml)
	 * is added to the runs HashMap so that these array ids can 
	 * be referenced as strings in createGroupList() and 
	 * createChildList().
	 */
	private void addStringRuns()
	{
		// weekday stops
		runs.put( "a4stop1", R.array.a4stop1);
	    runs.put( "a4stop2", R.array.a4stop2);
	    runs.put( "a4stop3", R.array.a4stop3);
	    runs.put( "a4stop4", R.array.a4stop4);
	    runs.put( "a4stop5", R.array.a4stop5);
	    runs.put( "a4stop6", R.array.a4stop6);
	    runs.put( "a4stop7", R.array.a4stop7);
	    runs.put( "a4stop8", R.array.a4stop8);
	    runs.put( "a4stop9", R.array.a4stop9);
	    runs.put( "a4stop10", R.array.a4stop10);
	    runs.put( "a4stop11", R.array.a4stop11);
	    runs.put( "a4stop12", R.array.a4stop12);
	    runs.put( "a4stop13", R.array.a4stop13);
	    runs.put( "a4stop14", R.array.a4stop14);
	    runs.put( "a4stop15", R.array.a4stop15);
	    runs.put( "a4stop16", R.array.a4stop16);
	    runs.put( "a4stop17", R.array.a4stop17);
	    runs.put( "a4stop18", R.array.a4stop18);
	    runs.put( "a4stop19", R.array.a4stop19);
	    runs.put( "a4stop20", R.array.a4stop20);
	    runs.put( "a4stop21", R.array.a4stop21);
	    runs.put( "a4stop22", R.array.a4stop22);
	    runs.put( "a4stop23", R.array.a4stop23);
	    runs.put( "a4stop24", R.array.a4stop24);
	    runs.put( "a4stop25", R.array.a4stop25);
	    runs.put( "a4stop26", R.array.a4stop26);
	    runs.put( "a4stop27", R.array.a4stop27);
	    runs.put( "a4stop28", R.array.a4stop28);
	    runs.put( "a4stop29", R.array.a4stop29);
	    runs.put( "a4stop30", R.array.a4stop30);
	    runs.put( "a4stop31", R.array.a4stop31);
	    runs.put( "a4stop32", R.array.a4stop32);
	    runs.put( "a4stop33", R.array.a4stop33);
	    runs.put( "a4stop34", R.array.a4stop34);
	    runs.put( "a4stop35", R.array.a4stop35);
	    runs.put( "a4stop36", R.array.a4stop36);
	    runs.put( "a4stop37", R.array.a4stop37);
	    runs.put( "a4stop38", R.array.a4stop38);
	    runs.put( "a4stop39", R.array.a4stop39);
	    runs.put( "a4stop40", R.array.a4stop40);
	    runs.put( "a4stop41", R.array.a4stop41);
	    runs.put( "a4stop42", R.array.a4stop42);
	    runs.put( "a4stop43", R.array.a4stop43);
	    runs.put( "a4stop44", R.array.a4stop44);
	    
	    // weekend stops
	    runs.put( "a4wstop1", R.array.a4wstop1);
	    runs.put( "a4wstop2", R.array.a4wstop2);
	    runs.put( "a4wstop3", R.array.a4wstop3);
	    runs.put( "a4wstop4", R.array.a4wstop4);
	    runs.put( "a4wstop5", R.array.a4wstop5);
	    runs.put( "a4wstop6", R.array.a4wstop6);
	    runs.put( "a4wstop7", R.array.a4wstop7);
	    runs.put( "a4wstop8", R.array.a4wstop8);
	    runs.put( "a4wstop9", R.array.a4wstop9);
	    runs.put( "a4wstop10", R.array.a4wstop10);
	    runs.put( "a4wstop11", R.array.a4wstop11);
	    runs.put( "a4wstop12", R.array.a4wstop12);
	    runs.put( "a4wstop13", R.array.a4wstop13);
	    runs.put( "a4wstop14", R.array.a4wstop14);
	    runs.put( "a4wstop15", R.array.a4wstop15);
	    runs.put( "a4wstop16", R.array.a4wstop16);
	    runs.put( "a4wstop17", R.array.a4wstop17);
	    runs.put( "a4wstop18", R.array.a4wstop18);
	    runs.put( "a4wstop19", R.array.a4wstop19);
	    runs.put( "a4wstop20", R.array.a4wstop20);
	    runs.put( "a4wstop21", R.array.a4wstop21);
	    runs.put( "a4wstop22", R.array.a4wstop22);
	    runs.put( "a4wstop23", R.array.a4wstop23);
	    runs.put( "a4wstop24", R.array.a4wstop24);
	    runs.put( "a4wstop25", R.array.a4wstop25);
	    runs.put( "a4wstop26", R.array.a4wstop26);
	    runs.put( "a4wstop27", R.array.a4wstop27);
	    runs.put( "a4wstop28", R.array.a4wstop28);
	    runs.put( "a4wstop29", R.array.a4wstop29);
	    runs.put( "a4wstop30", R.array.a4wstop30);
	    runs.put( "a4wstop31", R.array.a4wstop31);
	    runs.put( "a4wstop32", R.array.a4wstop32);
	    runs.put( "a4wstop33", R.array.a4wstop33);
	    runs.put( "a4wstop34", R.array.a4wstop34);
	    runs.put( "a4wstop35", R.array.a4wstop35);
	    runs.put( "a4wstop36", R.array.a4wstop36);
	    runs.put( "a4wstop37", R.array.a4wstop37);
	    runs.put( "a4wstop38", R.array.a4wstop38);
	    runs.put( "a4wstop39", R.array.a4wstop39);
	    runs.put( "a4wstop40", R.array.a4wstop40);
	    runs.put( "a4wstop41", R.array.a4wstop41);
	    runs.put( "a4wstop42", R.array.a4wstop42);
	    runs.put( "a4wstop43", R.array.a4wstop43);
	    runs.put( "a4wstop44", R.array.a4wstop44);
	    
	    // reduced service stops
	    runs.put( "a4rstop1", R.array.a4rstop1);
	    runs.put( "a4rstop2", R.array.a4rstop2);
	    runs.put( "a4rstop3", R.array.a4rstop3);
	    runs.put( "a4rstop4", R.array.a4rstop4);
	    runs.put( "a4rstop5", R.array.a4rstop5);
	    runs.put( "a4rstop6", R.array.a4rstop6);
	    runs.put( "a4rstop7", R.array.a4rstop7);
	    runs.put( "a4rstop8", R.array.a4rstop8);
	    runs.put( "a4rstop9", R.array.a4rstop9);
	    runs.put( "a4rstop10", R.array.a4rstop10);
	    runs.put( "a4rstop11", R.array.a4rstop11);
	    runs.put( "a4rstop12", R.array.a4rstop12);
	    runs.put( "a4rstop13", R.array.a4rstop13);
	    runs.put( "a4rstop14", R.array.a4rstop14);
	    runs.put( "a4rstop15", R.array.a4rstop15);
	    runs.put( "a4rstop16", R.array.a4rstop16);
	    runs.put( "a4rstop17", R.array.a4rstop17);
	    runs.put( "a4rstop18", R.array.a4rstop18);
	    runs.put( "a4rstop19", R.array.a4rstop19);
	    runs.put( "a4rstop20", R.array.a4rstop20);
	    runs.put( "a4rstop21", R.array.a4rstop21);
	    runs.put( "a4rstop22", R.array.a4rstop22);
	    runs.put( "a4rstop23", R.array.a4rstop23);
	    runs.put( "a4rstop24", R.array.a4rstop24);
	    runs.put( "a4rstop25", R.array.a4rstop25);
	    runs.put( "a4rstop26", R.array.a4rstop26);
	    runs.put( "a4rstop27", R.array.a4rstop27);
	    runs.put( "a4rstop28", R.array.a4rstop28);
	    runs.put( "a4rstop29", R.array.a4rstop29);
	    runs.put( "a4rstop30", R.array.a4rstop30);
	    runs.put( "a4rstop31", R.array.a4rstop31);
	    runs.put( "a4rstop32", R.array.a4rstop32);
	    runs.put( "a4rstop33", R.array.a4rstop33);
	    runs.put( "a4rstop34", R.array.a4rstop34);
	    runs.put( "a4rstop35", R.array.a4rstop35);
	    runs.put( "a4rstop36", R.array.a4rstop36);
	    runs.put( "a4rstop37", R.array.a4rstop37);
	    runs.put( "a4rstop38", R.array.a4rstop38);
	    runs.put( "a4rstop39", R.array.a4rstop39);
	    runs.put( "a4rstop40", R.array.a4rstop40);
	    runs.put( "a4rstop41", R.array.a4rstop41);
	    runs.put( "a4rstop42", R.array.a4rstop42);
	    runs.put( "a4rstop43", R.array.a4rstop43);
	    runs.put( "a4rstop44", R.array.a4rstop44);
	}
	
	/**
	 * If the spinner item that was selected is different from the current
	 * spinner page, then it goes to the appropriate page.
	 *
	 */
	public class MyOnItemSelectedListener implements OnItemSelectedListener
	{		
	    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
	    	Intent i;
		    
	    	if( pos != spinnerState )
	    	{
	    		i = new Intent();
	    		i.setClass( ct, Route4Activity.class);
	    		i.putExtra("timeframe", getPos(pos));
	    		i.putExtra("tab", tabNum);
	            startActivityForResult(i, 0);
	    	}
	    }

	    public void onNothingSelected(AdapterView<?> parent) {
	    	// Do nothing.
	    }
	}
}
